package com.hp.goods_service.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hp.common_api.entity.Goods;
import com.hp.common_api.entity.PageResult;
import com.hp.common_api.entity.Spu;
import com.hp.goods_service.service.ISpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * SPU商品的控制器
 */
@RestController
public class SpuController {

    @Autowired
    private ISpuService spuService;

    @GetMapping("/spus")
    public ResponseEntity<PageResult<Spu>> getSpuPage(
            @RequestParam(value = "current",required = false,defaultValue = "1") Long current){
        IPage<Spu> spuPage = spuService.getSpuPage(new Page<>(current, 10));
        return ResponseEntity.ok(new PageResult<Spu>(spuPage.getTotal(),spuPage.getCurrent(),
                spuPage.getRecords(),spuPage.getSize()));
    }

    @PostMapping("/spu")
    public ResponseEntity<Goods> saveSpu(@RequestBody Goods goods){
        spuService.saveSpu(goods);
        return ResponseEntity.ok(goods);
    }

    @GetMapping("/spu/{spuId}")
    public ResponseEntity<Spu> getSpuById(@PathVariable Long spuId){
        return ResponseEntity.ok(spuService.getById(spuId));
    }
}
